package com.example.wuqujingdianphone.fragment.bookcity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.wuqujingdianphone.R;
import com.example.wuqujingdianphone.activity.AudioListActivity;
import com.example.wuqujingdianphone.activity.VideoListActivity;
import com.example.wuqujingdianphone.activity.VideoMoreActivity;
import com.example.wuqujingdianphone.activity.VideoPlayActivity;
import com.example.wuqujingdianphone.adapter.BCMultiMediaAdapter;
import com.example.wuqujingdianphone.adapter.SectionAdapter;
import com.example.wuqujingdianphone.api.ApiConfig;
import com.example.wuqujingdianphone.base.BaseFragment;
import com.example.wuqujingdianphone.bean.MultiMediaBean;
import com.example.wuqujingdianphone.bean.MySectionBean;
import com.example.wuqujingdianphone.bean.MySectionChildBean;
import com.example.wuqujingdianphone.bean.TiYanguanBean;
import com.example.wuqujingdianphone.utils.GsonUtil;
import com.example.wuqujingdianphone.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.http.mode.CacheMode;
import com.vise.xsnow.http.mode.CacheResult;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class TIyanGuanFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, SectionAdapter.HeaderCallBack {
    private static final String TAG = "TIyanGuanFragment";
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private BCMultiMediaAdapter mAdapter;
    private String type;

    private boolean isFirstLoadDate;

    private List<TiYanguanBean> secondList = new ArrayList<>();
    private List<MySectionBean> mData = new ArrayList<>();
    private SectionAdapter sectionAdapter;

    public static TIyanGuanFragment getTIyanGuanFragment(String type) {
        TIyanGuanFragment fragment = new TIyanGuanFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tiyan, null);
        type = getArguments().getString("type");
        return view;
    }

    @Override
    protected void initData() {
        initView();
        isFirstLoadDate = true;

    }

    @Override
    protected void lazyLoad() {
        Log.i(TAG, "lazyLoad: type=" + type);
        if (isFirstLoadDate) {
            isFirstLoadDate = false;
            getData(type);
        }
    }

    private void initView() {
//        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
//        mAdapter = new BCMultiMediaAdapter(getActivity(), null);
//        mAdapter.openLoadAnimation();
//        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        // mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity()));
        sectionAdapter = new SectionAdapter(R.layout.item_book_list, R.layout.def_section_head, mData);
        sectionAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(sectionAdapter);
        sectionAdapter.setHeaderCallBack(this);
//        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Log.i(TAG, "onItemClick: position=" + position);
////                Intent intent = new Intent(getActivity(), BookWebDetailActivity.class);
////                intent.putExtra("type", type);
////                intent.putExtra("bookId", mAdapter.getItem(position).id);
////                startActivity(intent);
//                if (type.equals("听书馆")) {
//                    Intent intent = new Intent(getActivity(), AudioListActivity.class);
//                    intent.putExtra("id", mAdapter.getItem(position).id);
//                    intent.putExtra("title", mAdapter.getItem(position).news_tit);
//                    intent.putExtra("bac", ApiConfig.APP_URL + mAdapter.getItem(position).spic);
//                    startActivity(intent);
//                }
//
//                if (type.equals("体验馆")) {
//                    Intent intent = new Intent(getActivity(), VideoListActivity.class);
//                    intent.putExtra("postion", position);
//                    intent.putExtra("title", mAdapter.getItem(position).news_tit);
//                    startActivity(intent);
//                }
//
//
//            }
//        });


    }

    private void getData(String type) {
        showLoading(getActivity());
        OkGo.<String>get(ApiConfig.APP_URL + "index.php/jiekou/getVideoClass")
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        cancelLoading();
                        String sdsa = response.body();
                        secondList.clear();
                        secondList = GsonUtil.getObjectList(response.body(), TiYanguanBean.class);
                        getData();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        cancelLoading();
                        ToastUtil.show("" + response.getException().getMessage());
                    }
                });

    }

    private void getData() {
        for (int k = 0; k < secondList.size(); k++) {
            mData.add(new MySectionBean(true, secondList.get(k).getName(), secondList.get(k).getId(), true));
            for (int w = 0; w < secondList.get(k).getChilds().size(); w++) {
                mData.add(new MySectionBean(new MySectionChildBean(secondList.get(k).getChilds().get(w).getId(),
                        String.valueOf("http://www.wenqujingdian.com" + secondList.get(k).getChilds().get(w).getSpic()),
                        secondList.get(k).getChilds().get(w).getNews_tit(), false, secondList.get(k).getChilds().get(w).getMulu())));
            }
        }

        sectionAdapter.notifyDataSetChanged();
    }

//    private ArrayList<TiYanguanBean.ChildsBean.MuluBean> muluBeanList = new ArrayList<>();

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

        if (!mData.get(position).isHeader) {
            Bundle bundle = new Bundle();
            bundle.putParcelableArrayList("listFrom", mData.get(position).t.getList());
            bundle.putString("title", mData.get(position).t.getName());
            Intent intent = new Intent(getActivity(), VideoListActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }

    @Override
    public void headerclick(String id, String title) {
        Intent intent = new Intent(getActivity(), VideoMoreActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("id", id);
        startActivity(intent);
    }
}




